package com.pucong.gitee.database.backup.business.sys.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pucong.gitee.database.backup.business.service.mapper.ServiceLinuxLogMapper;
import com.pucong.gitee.database.backup.business.sys.mapper.SysRoleMapper;
import com.pucong.gitee.database.backup.business.sys.mapper.SysRoleResourceMapper;
import com.pucong.gitee.database.backup.business.sys.mapper.SysRoleUserMapper;
import com.pucong.gitee.database.backup.common.entity.service.ServiceLinuxLog;
import com.pucong.gitee.database.backup.common.entity.sys.*;
import com.pucong.gitee.database.backup.common.entity.sys.dto.SysRoleSaveDto;
import com.pucong.gitee.database.backup.common.entity.sys.vo.AddSysRoleResourceDto;
import com.pucong.gitee.database.backup.utils.UUID;
import com.pucong.gitee.database.backup.utils.UserRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户基础信息 service实现类
 *
 * @author pucong
 * @company gitee.com/pucong
 * @create 2020-12-01
 */
@Service
@Slf4j
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleResourceMapper sysRoleResourceMapper;

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Autowired
    private SysUserInfoService sysUserInfoService;

    @Override
    public List<SysRole> getSysRoleInfoList() {
        List<String> userId = sysUserInfoService.selectChildrenUserId();
        LambdaQueryWrapper<SysRole> params = new LambdaQueryWrapper<>();
        params.in(userId != null, SysRole::getCreateBy, userId);
        List<SysRole> sysRoles = sysRoleMapper.selectList(params);
        return sysRoles;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public SysRole insertSysRoleInfo(SysRoleSaveDto dto) {
        SysRole sysRole = new SysRole();
        UserJwt loginUser = UserRequest.getLoginUser();
        String uuid = UUID.uuid();
        sysRole.setUuid(uuid);
        sysRole.setRoleName(dto.getRoleName());
        sysRole.setCreateBy(loginUser.getUuid());
        sysRole.setCreateTime(new Date());
        sysRoleMapper.insert(sysRole);
        return sysRole;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int updateSysRoleInfoById(String id, SysRoleSaveDto dto) {
        SysRole sysRoleUpdate = new SysRole();
        sysRoleUpdate.setUuid(id);
        sysRoleUpdate.setRoleName(dto.getRoleName());
        return sysRoleMapper.updateById(sysRoleUpdate);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public int deleteSysRoleInfoById(String id) {
        int i = sysRoleMapper.deleteById(id);
        // 删除角色资源
        LambdaQueryWrapper<SysRoleResource> sysRoleResourceParams = new LambdaQueryWrapper<>();
        sysRoleResourceParams.eq(SysRoleResource::getRoleId, id);
        sysRoleResourceMapper.delete(sysRoleResourceParams);
        // 删除角色用户
        LambdaQueryWrapper<SysRoleUser> sysRoleUserParams = new LambdaQueryWrapper<>();
        sysRoleUserParams.eq(SysRoleUser::getRoleId, id);
        sysRoleUserMapper.delete(sysRoleUserParams);
        return i;
    }

    @Override
    public SysRoleResource addSysRoleResource(String id, AddSysRoleResourceDto resourceIds) {
        UserJwt loginUser = UserRequest.getLoginUser();
        SysRoleResource sysRoleResource = new SysRoleResource();
        String uuid = UUID.uuid();
        sysRoleResource.setUuid(uuid);
        sysRoleResource.setRoleId(id);
        sysRoleResource.setResourceId(resourceIds.getResourceId());
        sysRoleResource.setResourceType(resourceIds.getResourceType());
        sysRoleResource.setCreateBy(loginUser.getUuid());
        sysRoleResource.setCreateTime(new Date());
        sysRoleResourceMapper.insert(sysRoleResource);
        return sysRoleResource;
    }

    @Override
    public int deleteSysRoleResource(String roleId, String resourceId) {
        LambdaQueryWrapper<SysRoleResource> params = new LambdaQueryWrapper<>();
        params.eq(SysRoleResource::getRoleId, roleId);
        params.eq(SysRoleResource::getResourceId, resourceId);
        int delete = sysRoleResourceMapper.delete(params);
        return delete;
    }

    @Override
    public List<String> getRoleResourceByRoleId(String id) {
        LambdaQueryWrapper<SysRoleResource> params = new LambdaQueryWrapper<>();
        params.eq(SysRoleResource::getRoleId, id);
        List<SysRoleResource> sysRoleResources = sysRoleResourceMapper.selectList(params);
        List<String> list = sysRoleResources.stream().map(SysRoleResource::getResourceId).collect(Collectors.toList());
        return list;
    }
}
